# 创建时间：2025/5/10 星期六 14:44
# 创建人：李德才
from email.policy import default

import pymysql


class MySQLHelper:
    mysqlConnection = {
        "host": "localhost",
        "user": "root",
        "password": "123456",
        "database": "archser",
        "charset": "utf8",
        "port": 3306
    }

    def __init__(self, connectMap=mysqlConnection):
        self.host = connectMap["host"]
        self.user = connectMap["user"]
        self.password = connectMap["password"]
        self.database = connectMap["database"]
        self.charset = connectMap["charset"]
        self.port = connectMap["port"]
        self.conn = None
        self.cursor = None

    def __connect(self):
        """ 连接数据库 """
        self.conn = pymysql.connect(host=self.host, user=self.user, password=self.password, database=self.database,
                                    charset=self.charset, port=self.port)
        self.cursor = self.conn.cursor()

    def findFirst(self, sql, params=None):
        try:
            self.__connect()
            """ 查询一条数据 """
            if params is None:
                self.cursor.execute(sql)
            else:
                self.cursor.execute(sql, params)
            return self.cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            self.close()

    def findAll(self, sql, params=None):
        """ 查询多条数据 """
        try:
            self.__connect()
            if params is None:
                self.cursor.execute(sql)
            else:
                self.cursor.execute(sql, params)
            """ 查询一条数据 """
            return self.cursor.fetchall()
        except Exception as e:
            print(e)
        finally:
            self.close()

    def findMany(self, sql, params=None, size=1):
        """ 查询多条数据 """
        try:
            self.__connect()
            if params is None:
                self.cursor.execute(sql)
            else:
                self.cursor.execute(sql, params)
            return self.cursor.fetchmany(size)
        except Exception as e:
            print(e)
        finally:
            self.close()

    def update(self, sql, params=None):
        """ 修改数据 """
        try:
            self.__connect()
            """ 修改数据 """
            if params is None:
                self.cursor.execute(sql)
            else:
                self.cursor.execute(sql, params)
            self.conn.commit()
        except Exception as e:
            print(e)
            self.conn.rollback()
        finally:
            self.close()

    def delete(self, sql, params=None):
        """ 删除数据 """
        try:
            self.__connect()
            """ 删除数据 """
            if params is None:
                self.cursor.execute(sql)
            else:
                self.cursor.execute(sql, params)
            self.conn.commit()
        except Exception as e:
            print(e)
            self.conn.rollback()
        finally:
            self.close()

    def close(self):
        """ 关闭数据库连接 """
        self.cursor.close()
        self.conn.close()

    def __exit__(self, exc_type, exc_val, exc_tb):
        """ 退出with语句时自动关闭数据库连接 """
        self.close()

    def __enter__(self):
        return self  # 返回 self
